<?php
namespace api\know\controller;

use api\know\controller\KnowPublicController;
use api\know\model\MaterialModel;
use api\know\model\TeachingMaterialContentModel;
use api\know\model\AssetModel;
use api\know\model\TeachingMaterialKnowledgePointModel;
use api\know\model\TeachingMaterialNewWordModel;
use api\know\model\TeachingMaterialGrammarModel;
use api\know\model\KnowledgePointModel;
use api\know\model\NewWordModel;
use api\know\model\TeachingMaterialListenModel;
use api\know\model\PaperModel;
use api\know\model\GrammarModel;

class TeachingMaterialController extends KnowPublicController
{
    public function getList()
    {
        $grade = $this->request->param('grade', '');
        $subject = $this->request->param('subject', '');
		$version = $this->request->param('version', '');
        $volume = $this->request->param('volume', '');
		
        $w['material_type'] = 'teach';
        $w['is_delete'] = 0;
        if ($grade)
        {
            $w['grade_code'] = $grade;
        }
        if ($subject)
        {
            $w['subject_code'] = $subject;
        }
        if ($version)
        {
            $w['textbook_version_code'] = $version;
        }
        if ($volume)
        {
            $w['textbook_volume_code'] = $volume;
        }

        $rows = MaterialModel::where($w)->field([
            'id', 'name', 'uuid'
        ])->select();

        $this->success('请求成功!', $rows);
    }

    public function detail()
    {
        $chapter_uuid = $this->request->param('uuid', '');
		if (empty($chapter_uuid))
        {
            $this->error('章节不能为空!');
        }

        $w = ['chapter_uuid' => $chapter_uuid];
        $content = TeachingMaterialContentModel::where($w)
                    ->field(TeachingMaterialContentModel::$QueryField)
                    ->find();

        if ($content)
        {
            $content['content'] = $this->decodeAndReplaceHost($content['content']);
            $content['teaching_reference'] = $this->decodeAndReplaceHost($content['teaching_reference']);
            $content['eng_chn'] = $this->decodeAndReplaceHost($content['eng_chn']);
            
            if ($content['audio_asset_id'])
            {
                $audio_asset = AssetModel::find($content['audio_asset_id']);
                $content['audio_asset_url'] = $audio_asset ? cmf_get_file_download_url($audio_asset["file_path"]) : '';
            }
            else
            {
                $content['audio_asset_url'] = ''; 
            }
    
            if ($content['new_word_asset_id'])
            {
                $listen_asset = AssetModel::find($content['new_word_asset_id']);
                $content['new_word_asset_url'] = $listen_asset ? cmf_get_file_download_url($listen_asset["file_path"]) : '';
            }
            else
            {
                $content['new_word_asset_url'] = ''; 
            }
        }

        $kpt = TeachingMaterialKnowledgePointModel::where([
            'chapter_uuid' => $chapter_uuid
        ])->field(TeachingMaterialKnowledgePointModel::$QueryField)->select();

        $kpm_wh[] = ['uuid', 'in', array_map(function ($item) {
            return $item['knowledge_point_uuid'];
        }, $kpt->toArray())];
        $kpm_wh[] = ['is_delete', '=', 0];
        $kpm_wh[] = ['is_enable', '=', 1];

        $knowledge_point = KnowledgePointModel::where($kpm_wh)->field(KnowledgePointModel::$QueryField)->select();

        $nwt = TeachingMaterialNewWordModel::where([
            'chapter_uuid' => $chapter_uuid
        ])->field(TeachingMaterialNewWordModel::$QueryField)->select();

        $nw_wh[] = ['uuid', 'in', array_map(function ($item) {
            return $item['new_word_uuid'];
        }, $nwt->toArray())];
        $nw_wh[] = ['is_delete', '=', 0];
        $nw_wh[] = ['is_enable', '=', 1];

        $new_word = NewWordModel::where($nw_wh)->field(NewWordModel::$QueryField)->select();

        $listen = TeachingMaterialListenModel::where([
            'chapter_uuid' => $chapter_uuid,
			'is_delete' => 0,
			'is_enable' => 1
        ])->field(TeachingMaterialListenModel::$QueryField)->select()->toArray();
		
		foreach($listen as &$v)
		{
			$v['content'] = $this->decodeAndReplaceHost($v['content']);
			$v['analysis'] = $this->decodeAndReplaceHost($v['analysis']);
			if ($v['audio_asset_id'])
            {
                $audio_asset = AssetModel::find($v['audio_asset_id']);
                $v['audio_asset_url'] = $audio_asset ? cmf_get_file_download_url($audio_asset["file_path"]) : '';
            }
			else
			{
				$v['audio_asset_url'] = ''; 
			}
		}
		
        $paper = PaperModel::where([
            'chapter_uuid' => $chapter_uuid,
			'is_delete' => 0,
			'is_enable' => 1
        ])->field(PaperModel::$QueryField)->find();
		
        $grammars = TeachingMaterialGrammarModel::where([
            'chapter_uuid' => $chapter_uuid
        ])->field(TeachingMaterialGrammarModel::$QueryField)->select()->toArray();
		$grammar_uuids = array_map(function($item) {
            return $item['grammar_uuid'];
        }, $grammars);
		
        if (empty($grammar_uuids) === false)
        {
            $g_wh[] = ['is_delete', '=', 0];
            $g_wh[] = ['is_enable', '=', 1];
            $g_wh[] = ['uuid', 'in', $grammar_uuids];
    
            $grammar = GrammarModel::where($g_wh)->field(GrammarModel::$QueryField)->select();
		         
        }
        else
        {
            $grammar = [];
        }
        

        $this->success('请求成功!', [
            'content' => $content,
            'knowledge_point' => $knowledge_point,
            'new_word' => $new_word,
			'listen' => $listen,
            'paper' => $paper,
            'grammar' => $grammar,
        ]);
    }

}
